﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BSC_OTC_Management.Forms
{
    public partial class Frm_ImportSignal : Form
    {
        #region khai báo biến
        DataTable listImage = new DataTable();
        Code.CL_Ultility _cl = new Code.CL_Ultility();
        #endregion
        public Frm_ImportSignal()
        {
            InitializeComponent();

            //set title
            this.lb_title.Text = "+)Chọn 1 hoặc nhiều file chữ ký cần import." +
                Environment.NewLine + "+)Quy định đặt tên file theo Mã KH." +
                Environment.NewLine + "Ví dụ: Khách hàng Nguyễn Thành Đạt có mã KH 80 -> 80.jpg";

            //gán tên cho table
            this.listImage.TableName = "Signals";
            //add 2 column makh and column signal data
            this.listImage.Columns.Add("MAKH");
            this.listImage.Columns.Add("MAUDAU_IMAGE");
        }

        #region Event
        private void bttBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "Chọn file";
            openFileDialog.Filter = "Images (*.PNG;*.JPG)|*.PNG;*.JPG|All files (*.*)|*.*";
            openFileDialog.Multiselect = true;
            DialogResult dr = openFileDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                
                //clear table
                this.listImage.Clear();
                // Read the files
                foreach (String file in openFileDialog.FileNames)
                {
                    // Create a PictureBox.
                    DataRow row = this.listImage.NewRow();
                    try
                    {
                        //lay ra makh tu ten file
                        string namefile = Path.GetFileName(file);
                        string []list = namefile.Split('.');
                        row["MAKH"] = list[0];

                        FileStream imgFileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
                        int imgLenght = (int)imgFileStream.Length;
                        byte[] imgData = new byte[imgFileStream.Length];
                        imgFileStream.Read(imgData, 0, System.Convert.ToInt32(imgFileStream.Length));
                        imgFileStream.Close();
                        string stringData = Convert.ToBase64String(imgData, Base64FormattingOptions.InsertLineBreaks);
                        row["MAUDAU_IMAGE"] = stringData;

                        this.listImage.Rows.Add(row);

                        //hien thi danh sach  filename ra textbox
                        this.txtFilename.Text += "\"" + namefile +"\" ";

                    }
                    catch (Exception ex)
                    {
                        // Could not load the image - probably related to Windows file system permissions.
                        _cl._services.UPDATE_LOG_FILE("ERR", Code.StaticVariable.userID, "bttBrowser_Click", "Frm_ImportSignal()", ex.ToString());
                        MessageBox.Show(ex.Message,Code.StaticVariable.messeseboxLuuY);
                    }
                }
            }

        }

        private void bttImport_Click(object sender, EventArgs e)
        {
            try
            {
                bool result = _cl._services.Import_Signals(this.listImage,
                                             Code.StaticVariable.userID,
                                             Code.StaticVariable.menu_Code,
                                             (int)Code.StaticVariable.approveCase.ChuaDuyet +"",
                                             Code.StaticVariable.function_Edit);

                if (result)
                    MessageBox.Show(Code.StaticVariable.messeseboxImportSignalOK,Code.StaticVariable.messeseboxLuuY);
                else
                    MessageBox.Show(Code.StaticVariable.messeseboxThatBai, Code.StaticVariable.messeseboxLuuY);

            }
            catch (Exception ex)
            {
                _cl._services.UPDATE_LOG_FILE("ERR", Code.StaticVariable.userID, "bttImport_Click", "Frm_ImportSignal()", ex.ToString());   
                MessageBox.Show(ex.Message, Code.StaticVariable.messeseboxLuuY);
            }
        }

        private void bttExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
